package com.common.model.controller;

import cn.hutool.core.util.StrUtil;
import com.common.model.exception.CustomServiceError;
import com.common.model.result.Result;

/**
 * 公共父类controller
 * @author xianggj
 * @date 2021年8月27日17:24:40
 */
public class BaseController {

    /**
     * 成功返回模板
     * @param <T>
     * @return
     */
    public <T> Result<T> success() {
        Result.ResultBuilder<T> builder = Result.builder();
        builder.code(200);
        builder.msg("操作成功！");
        return builder.build();
    }

    /**
     * 返回成功 指定message
     * @param message
     * @param <T>
     * @return
     */
    public <T> Result<T> success(String message) {
        Result.ResultBuilder<T> builder = Result.builder();
        builder.code(200);
        if (StrUtil.isNotBlank(message)) {
            builder.msg(message);
        } else {
            builder.msg("操作成功！");
        }
        return builder.build();
    }

    /**
     * 返回成功，指定data数据
     * @param data
     * @param <T>
     * @return
     */
    public <T> Result<T> success(T data) {
        Result.ResultBuilder<T> builder = Result.builder();
        builder.code(200);
        builder.msg("操作成功！");
        builder.datas(data);
        return builder.build();
    }

    /**
     * 返回成功指定data 和 message数据
     * @param message
     * @param data
     * @param <T>
     * @return
     */
    public <T> Result<T> success(String message, T data) {
        Result.ResultBuilder<T> builder = Result.builder();
        builder.code(200);
        builder.datas(data);
        if (StrUtil.isNotBlank(message)) {
            builder.msg(message);
        } else {
            builder.msg("操作成功！");
        }
        return builder.build();
    }

    /**
     * 错误模板
     * @param message
     * @param <T>
     * @return
     */
    public <T> Result<T> error(String message) {
        Result.ResultBuilder<T> builder = Result.builder();
        // 预定义5000为无业务需求用户提示统一返回code
        builder.code(5000);
        if (StrUtil.isNotBlank(message)) {
            builder.msg(message);
        } else {
            builder.msg("操作失败！");
        }
        return builder.build();
    }

    /**
     * 设置自定义错误异常
     * @param opEnum
     * @param <T>
     * @return
     */
    public <T> Result<T> error(CustomServiceError opEnum) {
        Result.ResultBuilder<T> builder = Result.builder();

        if(null == opEnum){
            // 预定义5000为无业务需求用户提示统一返回code
            builder.code(5000);
            builder.msg("操作失败！");
            return builder.build();
        }

        builder.code(opEnum.getCode());
        if (StrUtil.isNotBlank(opEnum.getMessage())) {
            builder.msg(opEnum.getMessage());
        } else {
            builder.msg("操作失败！");
        }
        return builder.build();
    }

    /**
     * 自定义错误状态码
     * @param <T>
     * @return
     */
    public <T> Result<T> error(int code, String message) {
        Result.ResultBuilder<T> builder = Result.builder();
        builder.code(code);
        if (StrUtil.isNotBlank(message)) {
            builder.msg(message);
        } else {
            builder.msg("操作失败！");
        }
        return builder.build();
    }
    /**
     * 根据true/fasle返回成功或者失败
     * @param <T>
     * @return
     */
    public <T> Result<T> status(boolean successed) {
        Result.ResultBuilder<T> builder = Result.builder();
        if (successed) {
            builder.code(200);
            builder.msg("操作成功！");
        } else {
            builder.code(5000);
            builder.msg("操作失败！");
        }
        return builder.build();
    }

}
